package org.wangzs.blog.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.wangzs.blog.entity.Page;

public class Func {
	
	/**
	 * 将byte数组转换为字符串
	 * @param bts
	 * @return
	 */
	private static String bytes2Hex(byte[] bts) {
		String des = "";
		String tmp = null;
		for (int i = 0; i < bts.length; i++) {
			tmp = (Integer.toHexString(bts[i] & 0xFF));
			if (tmp.length() == 1) {
				des += "0";
			}
			des += tmp;
		}
		return des;
	}
	
	/**
	 * 对字符串进行sha1加密
	 * @param str 需要加密的字符串
	 * @return 加密后的字符串
	 */
	public static String sha1Encrypt(String str) {
		MessageDigest md;
		String newStr = null;
		try {
			md = MessageDigest.getInstance("sha1");
			md.update(str.getBytes());
			newStr = bytes2Hex(md.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		return newStr;
	}
	
	/**
	 * 对字符串进行md5加密
	 * @param str 需要加密的字符串
	 * @return 加密后的字符串
	 */
	public static String md5Encrypt(String str) {
		MessageDigest md;
		String newStr = null;
		try {
			md = MessageDigest.getInstance("md5");
			md.update(str.getBytes());
			newStr = bytes2Hex(md.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		return newStr;
	}
	
	public static void clearCookie(HttpServletRequest request, HttpServletResponse response) {
		for(Cookie cookie : request.getCookies()) {
			cookie.setValue(null);
			cookie.setMaxAge(0);
			response.addCookie(cookie);
		}
	}
	
	public static String getPagination(Page<?> page){
		return null;
	}
}
